package com.shkg.opencloud.fastdfs.util;

import java.io.*;
import java.nio.charset.Charset;

import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.shkg.opencloud.fastdfs.config.FileSystemConfig;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

/**
 * FastDFS 客户端
 */
@Component
public class FastDFSClient {

    private static final String SLANT = "/";

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FileSystemConfig fileSystemConfig;

    /**
     * 1.上传文件
     * 2.返回路劲需要入库保存文件信息保存
     *
     * @param file
     * @return java.lang.String
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);
        return storePath.getGroup() + SLANT + storePath.getPath();
    }

    /**
     * 1.上传文件
     * 2.返回路劲需要入库保存文件信息保存
     *
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(File file) throws IOException {
        FileInputStream inputStream = new FileInputStream(file);
        StorePath storePath = storageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
        return getResAccessUrl(storePath);
    }

    // 封装图片完整URL地址
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = fileSystemConfig.getFileFullPath(storePath.getFullPath());
        return fileUrl;
    }


    /**
     * 1.上传图片并生成缩略图
     * 2.返回路劲需要入库保存文件信息保存
     *
     * @param file
     * @return java.lang.String
     */
    public String uploadImageWithThumbImage(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()), null);

        return storePath.getGroup() + SLANT + storePath.getPath();
    }

    /**
     * 将一段字符串生成一个文件上传
     *
     * @param content
     * @param fileExtension
     * @return java.lang.String
     */
    public String uploadFile(String content, String fileExtension) throws IOException {
        byte[] buff = content.getBytes(Charset.forName("UTF-8"));
        try (ByteArrayInputStream stream = new ByteArrayInputStream(buff)) {
            StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
            return storePath.getGroup() + SLANT + storePath.getPath();
        }
    }

    /**
     * 下载文件
     *
     * @param fileUrl 文件url    group1/M00/00/00/wKg7g1wN2YyAAH1MAADJbaKmScw004.jpg
     * @return
     */
    public byte[] download(String fileUrl) {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        byte[] bytes = storageClient.downloadFile(group, path, new DownloadByteArray());
        return bytes;
    }


    /**
     * 删除文件
     *
     * @param fileUrl 文件地址：以group开头
     * @return
     */
    public void deleteFile(String fileUrl) throws FdfsUnsupportStorePathException {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            throw e;
        }
    }


}
